#include "mycoin.h"
#include <QPixmap>
#include <QDebug>
//MyCoin::MyCoin(QWidget *parent) : QPushButton(parent)
//{

//}
MyCoin::MyCoin (QString imgPath)
{
    QPixmap pix;
    bool ret=pix.load(imgPath);
    if(!ret){
        QString str=QString("%1图片加载失败").arg(imgPath);
        qDebug()<<str;
        return;
    }
    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));
    //初始化两个定时器   可以多次初始化，但不能重复定义
    this->timer1=new QTimer(this);
    this->timer2=new QTimer(this);
    //初始化min和max的值
    this->min=1;
    this->max=8;
    //监听正面翻反面的信号，并且反转金币为银币
    connect(timer1,&QTimer::timeout,[=](){
        QPixmap pixToBack;
        QString str=QString(":/Coin000%1.png").arg(this->min++);
        bool res=pixToBack.load(str);
        if(!res) qDebug()<<"图片加载失败！";
        this->setFixedSize(pixToBack.width(),pixToBack.height());
        this->setStyleSheet("QPushButton{border:0px}");
        this->setIcon(pixToBack);
        this->setIconSize(QSize(pixToBack.width(),pixToBack.height()));
        if(this->min>this->max){
            timer1->stop();
            this->isAnimation=false;
            this->min=1;
        }
    });
    //监听反面翻正面的信号，并且翻转银币为金币
    connect(timer2,&QTimer::timeout,[=](){
        QPixmap pixToFront;
        QString str=QString(":/Coin000%1.png").arg(this->max--);
        pixToFront.load(str);
        this->setFixedSize(pixToFront.width(),pixToFront.height());
        this->setStyleSheet("QPushButton{border:0px}");
        this->setIcon(pixToFront);
        this->setIconSize(QSize(pixToFront.width(),pixToFront.height()));
        if(this->max<this->min){
            timer2->stop();
            this->isAnimation=false;
            this->max=8;
        }
    });
}

void MyCoin::changeFlag()
{
    //负责正面翻转成反面
    if(this->flag){
        //开启timer1定时器
        timer1->start(30);
        this->isAnimation=true;
        this->flag=false;
    }
    //负责反面翻转成正面
    else
    {
        //开启timer2定时器
        timer2->start(30);
        this->isAnimation=true;
        this->flag=true;
    }
}

void MyCoin::mousePressEvent(QMouseEvent *event)
{
    if(isAnimation||this->isWin){
        return;
    }
    else{
        return QPushButton::mousePressEvent(event);
    }
}
